package com.ramos.kuaixiao.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.entity.Customer;
import com.ramos.kuaixiao.entity.CustomerStore;
import com.ramos.kuaixiao.ui.ShopDetailActivity;
import com.ramos.kuaixiao.utils.StringUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CustomerStoreAdapter extends RecyclerView.Adapter {

    private List<CustomerStore> tasklist;
    private Context context;
    Customer mCustomer;

    public CustomerStoreAdapter(Context context, List<CustomerStore> hotcourse, Customer customer) {
        this.context = context;
        this.tasklist = hotcourse;
        mCustomer = customer;
    }

    public void refresh(List<CustomerStore> data) {
        tasklist = data;
        sortDistance(tasklist);
        notifyDataSetChanged();
    }

    private void sortDistance(List<CustomerStore> tasklist) {
        List<CustomerStore> tem1 = new ArrayList<>();
        tem1.clear();
        List<CustomerStore> tem2 = new ArrayList<>();
        tem2.clear();
        for (CustomerStore customer : tasklist) {
            if (StringUtil.isEmpty(customer.getDistance())) {
                tem2.add(customer);
            } else {
                tem1.add(customer);
            }
        }
        Comparator<CustomerStore> comparator = new Comparator<CustomerStore>() {
            public int compare(CustomerStore s1, CustomerStore s2) {
                // 先排年龄
                if (Integer.valueOf(s1.getDistance()) != Integer.valueOf(s2.getDistance())) {
                    return Integer.valueOf(s1.getDistance()) - Integer.valueOf(s2.getDistance());
                } else if (!s1.getName().equals(s2.getName())) {
                    // 年龄相同则按姓名排序
                    return s1.getName().compareTo(s2.getName());
                } else {
                    return 1;
                }
            }
        };
        tasklist.clear();
        //这里就会自动根据规则进行排序
        Collections.sort(tem1, comparator);
        for (int i = 0; i < tem1.size(); i++) {
            CustomerStore stu = tem1.get(i);
            //Log.d("Collections", "dis:" + stu.getDistance());
        }
        tasklist.addAll(tem1);
        tasklist.addAll(tem2);
    }


    @SuppressLint("InflateParams")
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.listitem_customer, null);
        ViewHodler vh = new ViewHodler(view);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHodler viewHolder = (ViewHodler) holder;
        viewHolder.tvName.setText(tasklist.get(position).getName());
        viewHolder.tvAddress.setText(tasklist.get(position).getAddress());
        String dis = tasklist.get(position).getDistance();
        if (StringUtil.isEmpty(dis)) {
            viewHolder.tvDistance.setVisibility(View.GONE);
        } else {
            viewHolder.tvDistance.setVisibility(View.VISIBLE);
            viewHolder.tvDistance.setText(dis + "km");
        }
        viewHolder.icon.setVisibility(View.GONE);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomerStore customerStore = tasklist.get(position);
                Intent intent = new Intent(context, ShopDetailActivity.class);
                mCustomer.setHouseId(customerStore.getCustomerId());
                mCustomer.setHouseName(customerStore.getName());
                mCustomer.setAddress(customerStore.getAddress());
                mCustomer.setTelephone(customerStore.getMobilePhone());
                intent.putExtra("customer", mCustomer);
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return tasklist.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    class ViewHodler extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvAddress;
        TextView tvDistance;
        ImageView icon;
        public ViewHodler(View convertView) {
            super(convertView);
            tvName = (TextView) convertView.findViewById(R.id.name);
            tvAddress = (TextView) convertView.findViewById(R.id.address);
            tvDistance = (TextView) convertView.findViewById(R.id.distance);
            icon = (ImageView) convertView.findViewById(R.id.icon);
        }
    }
}